window.onload = () => {
  thscThemeObj.needChangeFlag = false;
  Vue.use(window.hxmui, {
    themeMode: 'light'
  });
  new Vue({
    el: '#app',
    data() {
      return {
        searchPattern: '',
        result: {},
        searchIsPending: false,
        monitorStocks: []
      }
    },
    methods: {
      handleIptChange() {
        if (this.searchPattern === '') {
          return;
        }
        this.searchIsPending = true;
        const pattern = this.searchPattern;
        $.ajax({
          type: "get",
          url: hxmConfig.baseApi,
          data: {
            op: 'searchStock',
            pattern: pattern
          },
          success: res => {
            if (res.code === 0) {
              this.setSearchResult(pattern, res.data.slice(0, 10));
            } else {
              this.$toast.error('搜索请求失败，请稍后再试');
            }
            this.searchIsPending = false;
          },
          error: err => {
            console.log(err);
            this.$toast.error('搜索请求失败，请稍后再试');
            this.searchIsPending = false;
          }
        });
      },
      setSearchResult(pattern, list) {
        if (pattern === '' || !list || !list.length) {
          return;
        }
        this.$set(this.result, pattern, []);
        for (let i = 0; i < list.length; i++) {
          const item = list[i];
          this.result[pattern].push({
            key: item.stockCode,
            code: item.stockCode,
            name: item.stockName,
            marketId: item.marketId,
            marketName: item.marketName
          })
        }
      },
      highlightParse(str) {
        const p = this.searchPattern;
        if (p === '') {
          return str;
        }
        const matchReg = new RegExp(`([~${p}]*)(${p}+)([~${p}]*)`);
        return str.replace(matchReg, (str, a, b, c) => {
          return `${a}<span class="fc-blue">${b}</span>${c}`;
        })
      },
      getMonitorStocks() {
        $.ajax({
          type: "get",
          url: hxmConfig.baseApi,
          data: {
            op: 'getEstimateLimitUpStock'
          },
          success: res => {
            if (res.code === 0) {
              this.initMonitorStocks(res.data.monitorStockList);
            } else {
              this.$toast.error('已监控股票获取失败，请稍后再试');
            }
          },
          error: err => {
            console.log(err);
            this.$toast.error('已监控股票获取失败，请稍后再试');
          }
        });
      },
      initMonitorStocks(stocks) {
        if (stocks && stocks.length) {
          for (let i = 0; i < stocks.length; i++) {
            this.monitorStocks.push(stocks[i].stockCode);
          }
        }
      },
      stockInMonitor(stockCode) {
        return this.monitorStocks.indexOf(stockCode) > -1;
      },
      setMonitor(stockCode) {
        if (this.setMonitorApiLock) {
          return;
        }
        const isAdd = !this.stockInMonitor(stockCode);
        const op = isAdd ? 'setStockMonitor' : 'cancelStockMonitor';
        const opTxt = isAdd ? '添加' : ' 取消';
        if (isAdd && this.monitorStocks.length >= 30) {
          this.$toast.warn('最多监控30只股票');
          return;
        }
        this.setMonitorApiLock = true;
        $.ajax({
          type: "get",
          url: hxmConfig.baseApi,
          data: {
            op,
            stockCode,
          },
          success: res => {
            if (res.code === 0) {
              this.$toast.success(`${opTxt}监控成功`);
            } else {
              this.$toast.error(`${opTxt}监控失败，请稍后再试`);
            }
            this.updateMonitor(stockCode);
            this.setMonitorApiLock = false;
          },
          error: err => {
            console.log(err);
            this.$toast.error(`${opTxt}监控失败，请稍后再试`);
            this.setMonitorApiLock = false;
          }
        });
      },
      updateMonitor(stockCode) {
        const idxInMonitorStocks = this.monitorStocks.indexOf(stockCode);
        if (idxInMonitorStocks > -1) {
          this.monitorStocks.splice(idxInMonitorStocks, 1);
        } else {
          this.monitorStocks.push(stockCode);
        }
      },
      jumpToDetail({code, marketId}) {
        location.href = `client.html?action=ymtz^url=${location.protocol}${hxmConfig.ztDetail}?code=${code}&marketId=${marketId}^webid=2804^mode=new`;
      }
    },
    created() {
      this.getMonitorStocks();
    }
  })
}